package adminSys

import (
	db "fiberAmisAdmin/config"
	"fiberAmisAdmin/controllers/amis"
	"github.com/gofiber/fiber/v2"
	"strconv"
	"time"
)

type Controller interface {
	List(c *fiber.Ctx) error
	Form() []map[string]interface{}
	Config() map[string]interface{}
}

type BaseSysController struct {
	Child Controller // 用于存储子类实例
}

// Index 显示所有用户列表的页面
func (bc *BaseSysController) Index(c *fiber.Ctx) error {
	child := bc.Child
	action := c.Query("_action")
	if action == "getData" {
		return child.List(c)
	}
	// 从配置中获取 pageSize 并转换为 int
	pageSize := child.Config()["pageSize"].(string)
	data, err := amis.TextControl(child.Form(), c.Path(), pageSize)
	if err != nil {
		return bc.returnError(c, fiber.StatusOK, "Error")
	}
	return c.Render("amis_page", fiber.Map{
		"AdminAmisJson": data,
	})
}

// Show 方法用于处理获取单个用户请求
func (bc *BaseSysController) Show(c *fiber.Ctx) error {
	child := bc.Child
	tableName := child.Config()["tableName"].(string)
	id, err := strconv.Atoi(c.Params("id"))
	if err != nil {
		return bc.returnError(c, fiber.StatusBadRequest, "无效的 ID")
	}

	var result map[string]interface{}
	err = db.MySQLDB.Table(tableName).Where("id =?", id).Find(&result).Error
	return bc.returnSuccess(c, fiber.StatusOK, "Success", result)
}

// Store 方法用于处理创建用户请求
func (bc *BaseSysController) Store(c *fiber.Ctx) error {
	child := bc.Child
	tableName := child.Config()["tableName"].(string)
	var params map[string]interface{}
	if err := c.BodyParser(&params); err != nil {
		return bc.returnError(c, fiber.StatusBadRequest, "参数解析失败不存在")
	}
	delete(params, "id")
	params["created_at"] = time.Now().Format("2006-01-02 15:04:05")
	params["updated_at"] = time.Now().Format("2006-01-02 15:04:05")
	result := db.MySQLDB.Table(tableName).Create(params)
	if result.Error != nil {
		return bc.returnError(c, fiber.StatusInternalServerError, "创建失败: "+result.Error.Error())
	}
	return bc.returnSuccess(c, fiber.StatusOK, "创建成功！", "")

}

// Update 方法用于处理更新用户请求
func (bc *BaseSysController) Update(c *fiber.Ctx) error {
	child := bc.Child
	tableName := child.Config()["tableName"].(string)
	var params map[string]interface{}
	id := c.Params("id")
	if err := c.BodyParser(&params); err != nil {
		return bc.returnError(c, fiber.StatusBadRequest, "参数解析失败")
	}
	params["updated_at"] = time.Now().Format("2006-01-02 15:04:05")
	result := db.MySQLDB.Table(tableName).Where("id = ?", id).Updates(params)
	if result.Error != nil {
		return bc.returnError(c, fiber.StatusInternalServerError, "更新失败: "+result.Error.Error())
	}
	return bc.returnSuccess(c, fiber.StatusOK, "更新成功！", "")
}

// Destroy 方法用于处理删除用户请求
func (bc *BaseSysController) Destroy(c *fiber.Ctx) error {
	child := bc.Child
	tableName := child.Config()["tableName"].(string)
	id := c.Params("id")
	result := db.MySQLDB.Table(tableName).Where("id = ?", id).Delete(nil)
	if result.Error != nil {
		return bc.returnError(c, fiber.StatusInternalServerError, "删除失败: "+result.Error.Error())
	}
	return bc.returnSuccess(c, fiber.StatusOK, "删除成功！", "")

}

// returnSuccess 返回成功的 API 响应
func (c *BaseSysController) returnSuccess(ctx *fiber.Ctx, status int, msg string, data interface{}) error {
	if status == 200 {
		status = 0
	}
	return ctx.JSON(APIResponse{
		Status: status,
		Msg:    msg,
		Data:   data,
	})
}

// returnError 返回失败的 API 响应
func (c *BaseSysController) returnError(ctx *fiber.Ctx, status int, msg string) error {
	return ctx.Status(status).JSON(APIResponse{
		Status: status,
		Msg:    msg,
	})
}

// APIResponse 用于统一格式化 API 返回结果
type APIResponse struct {
	Status int         `json:"status"`
	Msg    string      `json:"msg"`
	Data   interface{} `json:"data"`
}
